package com.photoeditor.demo.model.image.emoji;

import android.content.Context;

import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.model.share.ShareImageTools;


/**
 * 需要安装的Sticker Resource
 */
public class InstalledStickerResource extends StickerApkResource{

    public InstalledStickerResource(Context context, String pkgName, boolean needInit) {
        super(pkgName);
        init(context, needInit);
    }

    @Override
    public void init(Context context, boolean needInit) {
        //初始化外置安装Apk的信息
        if(ShareImageTools.getAppIsInstalled(context, mPkgName)){
            Context mApkContext = null;
            try {
                mApkContext = context.createPackageContext(mPkgName, Context.CONTEXT_IGNORE_SECURITY);
                if(mApkContext != null){
                    isExist = true;
                    mRes = mApkContext.getResources();
                    setName(mRes.getString(mRes.getIdentifier("app_name", "string", mPkgName)));
                    if(needInit) {
                        doInit();
                    }
                }
            } catch (Throwable e) {
                e.printStackTrace();
                isExist = false;
            }

        } else{
            isExist = false;
        }
    }

    @Override
    public void doInit() {
        if(!isInit()) {
            super.doInit();
            if (isExist()) {
                int id = mRes.getIdentifier("sticker_name", "array", mPkgName);
                setEmojiNames(mRes.getStringArray(id));
                setEmojiColNum(mRes.getInteger(mRes.getIdentifier("emoji_col_num", "integer", mPkgName)));
                setEmojiPageNum(mRes.getInteger(mRes.getIdentifier("emoji_page_num", "integer", mPkgName)));
                setVerticalSpacing(mRes.getDimensionPixelSize(mRes.getIdentifier("vertical_spacing", "dimen", mPkgName)));
                setHorizontalSpacing(mRes.getDimensionPixelSize(mRes.getIdentifier("horizontal_spacing", "dimen", mPkgName)));
                setEmojiWidth(mRes.getDimensionPixelSize(mRes.getIdentifier("emoji_width", "dimen", mPkgName)));
                setEmojiHeight(mRes.getDimensionPixelSize(mRes.getIdentifier("emoji_Height", "dimen", mPkgName)));
                setItemWidth(mRes.getDimensionPixelSize(mRes.getIdentifier("item_width", "dimen", mPkgName)));
                setItemHeight(mRes.getDimensionPixelSize(mRes.getIdentifier("item_height", "dimen", mPkgName)));
                setPageHeight(mRes.getDimensionPixelSize(mRes.getIdentifier("page_height", "dimen", mPkgName)));
                setEditEmojiWidth(mRes.getDimensionPixelSize(mRes.getIdentifier("edit_emoji_width", "dimen", mPkgName)));
                setEditEmojiHeight(mRes.getDimensionPixelSize(mRes.getIdentifier("edit_emoji_height", "dimen", mPkgName)));
                int bgColorId = mRes.getIdentifier("bg_color", "color", mPkgName);
                if(bgColorId != 0) {
                    setBgColor(mRes.getColor(bgColorId));
                } else {
                    setBgColor(ComponentContext.getContext().getResources().getColor(R.color.emoji_pannel_default_background_color));
                }

                setNeedBuy(false);
                setStickerIconId(mRes.getIdentifier(mRes.getString(mRes.getIdentifier("sticker_icon_name", "string", mPkgName)), "drawable", mPkgName));
            }
        }
    }
}
